package is.progetto2.gui;

import is.progetto2.activity.Activity;
import is.progetto2.activity.Bind;
import is.progetto2.activity.Job;
import is.progetto2.activity.ResultSchedule;
import is.progetto2.activity.Scheduler;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class ActivityGui {

	// -------------------------------LOGICA--------------------------
	private Activity activity;
	// -------------------------------GRAFICA--------------------------
	private JFrame frmAtticit;
	private JLabel lblLavori;
	private JLabel lblVincoli;
	private JButton btnNuovoLavori;
	private JButton btnModificaLavori;
	private JButton btnEliminaLavori;
	private JButton btnNuovoVincoli;
	private JButton btnModificaVincoli;
	private JButton btnEliminaVincoli;
	private JLabel lblSchedule;
	private JTable table;
	private JButton btnSchedula;
	private JScrollPane scrollPane;
	private JTable tabellaLavori;
	private JTable tabellaVincoli;
	private JScrollPane scrollPane_1;
	private JScrollPane scrollPane_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ActivityGui window = new ActivityGui();
					window.frmAtticit.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ActivityGui() {
		activity = new Activity();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmAtticit = new JFrame();
		frmAtticit.setTitle("Attivit\u00E0");
		frmAtticit.setBounds(100, 100, 599, 411);
		frmAtticit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmAtticit.getContentPane().setLayout(null);
		frmAtticit.getContentPane().add(getLblLavori());
		frmAtticit.getContentPane().add(getLblVincoli());
		frmAtticit.getContentPane().add(getBtnNuovoLavori());
		frmAtticit.getContentPane().add(getBtnModificaLavori());
		frmAtticit.getContentPane().add(getBtnEliminaLavori());
		frmAtticit.getContentPane().add(getBtnNuovoVincoli());
		frmAtticit.getContentPane().add(getBtnModificaVincoli());
		frmAtticit.getContentPane().add(getBtnEliminaVincoli());
		frmAtticit.getContentPane().add(getLblSchedule());
		frmAtticit.getContentPane().add(getScrollPane());
		frmAtticit.getContentPane().add(getBtnSchedula());
		frmAtticit.getContentPane().add(getScrollPane_1_1());
		frmAtticit.getContentPane().add(getScrollPane_2_1());
	}

	private JLabel getLblLavori() {
		if (lblLavori == null) {
			lblLavori = new JLabel("Lavori:");
			lblLavori.setBounds(6, 4, 55, 16);
		}
		return lblLavori;
	}

	private JLabel getLblVincoli() {
		if (lblVincoli == null) {
			lblVincoli = new JLabel("Vincoli:");
			lblVincoli.setBounds(408, 4, 55, 16);
		}
		return lblVincoli;
	}

	private JButton getBtnNuovoLavori() {
		if (btnNuovoLavori == null) {
			btnNuovoLavori = new JButton("Nuovo");
			btnNuovoLavori.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					new JobCreator(frmAtticit, activity, tabellaLavori);
				}
			});
			btnNuovoLavori.setBounds(184, 28, 90, 28);
		}
		return btnNuovoLavori;
	}

	private JButton getBtnModificaLavori() {
		if (btnModificaLavori == null) {
			btnModificaLavori = new JButton("Modifica");
			btnModificaLavori.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Job j = activity.getJob(tabellaLavori.getSelectedRow());
					if (j != null) {
						new JobCreator(frmAtticit, activity, j, tabellaLavori);
					}
				}
			});
			btnModificaLavori.setEnabled(false);
			btnModificaLavori.setBounds(184, 68, 90, 28);
		}
		return btnModificaLavori;
	}

	private JButton getBtnEliminaLavori() {
		if (btnEliminaLavori == null) {
			btnEliminaLavori = new JButton("Elimina");
			btnEliminaLavori.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Job j = activity.getJob(tabellaLavori.getSelectedRow());
					if (j != null) {
						activity.removeJob(j);
						tabellaLavori.setModel(new JobTableModel(activity));
					}
					btnModificaLavori.setEnabled(false);
					btnEliminaLavori.setEnabled(false);
				}
			});
			btnEliminaLavori.setEnabled(false);
			btnEliminaLavori.setBounds(184, 108, 90, 28);
		}
		return btnEliminaLavori;
	}

	private JButton getBtnNuovoVincoli() {
		if (btnNuovoVincoli == null) {
			btnNuovoVincoli = new JButton("Nuovo");
			btnNuovoVincoli.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					new BindCreator(frmAtticit, activity, tabellaVincoli);
				}
			});
			btnNuovoVincoli.setBounds(306, 28, 90, 28);
		}
		return btnNuovoVincoli;
	}

	private JButton getBtnModificaVincoli() {
		if (btnModificaVincoli == null) {
			btnModificaVincoli = new JButton("Modifica");
			btnModificaVincoli.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Bind b = activity.getBind(tabellaVincoli.getSelectedRow());
					if (b != null) {
						new BindCreator(frmAtticit, activity, b, tabellaVincoli);
					}
				}
			});
			btnModificaVincoli.setEnabled(false);
			btnModificaVincoli.setBounds(306, 68, 90, 28);
		}
		return btnModificaVincoli;
	}

	private JButton getBtnEliminaVincoli() {
		if (btnEliminaVincoli == null) {
			btnEliminaVincoli = new JButton("Elimina");
			btnEliminaVincoli.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Bind b = activity.getBind(tabellaVincoli.getSelectedRow());
					if (b != null) {
						activity.removeBind(b);
						tabellaVincoli.setModel(new BindTableModel(activity));
					}
					btnEliminaVincoli.setEnabled(false);
					btnModificaVincoli.setEnabled(true);
				}
			});
			btnEliminaVincoli.setEnabled(false);
			btnEliminaVincoli.setBounds(306, 108, 90, 28);
		}
		return btnEliminaVincoli;
	}

	private JLabel getLblSchedule() {
		if (lblSchedule == null) {
			lblSchedule = new JLabel("Cronologia:");
			lblSchedule.setBounds(6, 166, 90, 16);
		}
		return lblSchedule;
	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable();
		}
		return table;
	}

	private JButton getBtnSchedula() {
		if (btnSchedula == null) {
			btnSchedula = new JButton("Schedula");
			btnSchedula.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					System.out.println(activity);
					Scheduler s = new Scheduler(activity);
					s.resolve();
					ResultSchedule rs = s.getResult();
					System.out.println(rs);
					ScheduleTableModel stm = new ScheduleTableModel(rs);
					table.setModel(stm);
					stm.fireTableDataChanged();
				}
			});
			btnSchedula.setBounds(487, 160, 90, 28);
		}
		return btnSchedula;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(6, 194, 571, 173);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTabellaLavori() {
		if (tabellaLavori == null) {
			tabellaLavori = new JTable();
			tabellaLavori.setModel(new JobTableModel(activity));
			int clen = tabellaLavori.getColumnCount();
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer
					.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
			System.out.println(clen);
			for (int i = 0; i < clen; i++) {
				System.out.println("qui");
				tabellaLavori.getColumn(tabellaLavori.getColumnName(i)).setCellRenderer( centerRenderer );
			}
			tabellaLavori.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					Job j = activity.getJob(tabellaLavori.getSelectedRow());
					if (j != null) {
						btnEliminaLavori.setEnabled(true);
						btnModificaLavori.setEnabled(true);
					}
				}
			});
		}
		return tabellaLavori;
	}

	private JTable getTabellaVincoli() {
		if (tabellaVincoli == null) {
			tabellaVincoli = new JTable();
			tabellaVincoli.setModel(new BindTableModel(activity));
			tabellaVincoli.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					Bind b = activity.getBind(tabellaVincoli.getSelectedRow());
					if (b != null) {
						btnEliminaVincoli.setEnabled(true);
						btnModificaVincoli.setEnabled(true);
					}
				}
			});
		}
		return tabellaVincoli;
	}

	private JScrollPane getScrollPane_1_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(6, 32, 166, 98);
			scrollPane_1.setViewportView(getTabellaLavori());
		}
		return scrollPane_1;
	}

	private JScrollPane getScrollPane_2_1() {
		if (scrollPane_2 == null) {
			scrollPane_2 = new JScrollPane();
			scrollPane_2.setBounds(408, 34, 169, 102);
			scrollPane_2.setViewportView(getTabellaVincoli());
		}
		return scrollPane_2;
	}
}
